package com.simple.microservices.service.auth;


import cn.dev33.satoken.stp.StpLogic;
import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.simple.microservices.api.auth.AuthService;
import com.simple.microservices.api.dto.LoginDTO;
import com.simple.microservices.commons.utils.CommonRequestUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * 授权服务实现类
 *
 * @author HYF
 */
@Service
public class AuthServiceImpl implements AuthService {

    @Autowired
    @Qualifier("tokenRedis")
    private RedisTemplate redisTemplate;
    @Autowired
    @Qualifier("redis")
    private RedisTemplate redis;

    @Value("${sa-token.timeout}")
    private Long timeout;


    @Override
    public String login(LoginDTO loginDTO) {
        StpLogic stpLogic = new StpLogic(loginDTO.getIdentityType());
        StpUtil.setStpLogic(stpLogic);
        String device = CommonRequestUtils.getDevice();
        if (StringUtils.isNotEmpty(device)){
            //不同设备登录
            StpUtil.login(loginDTO.getCellphone(),device);
        }else {
            StpUtil.login(loginDTO.getCellphone());
        }
        String token = StpUtil.getTokenValueByLoginId(loginDTO.getCellphone());
        String accountId = loginDTO.getAccountId();


        return token;
    }


}